package model.jsf;

import com.etfbl.net.dao.*;
import com.etfbl.net.dto.*;
import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import models.jsf.base.BaseController;
import models.jsf.util.JsfUtil;
/**
 *
 * @author abosancic
 * @date Jul-29-2013
 */ 

@ManagedBean(name = "drzavaController")
@ViewScoped
public class DrzavaController extends BaseController<Drzava> implements Serializable {

    private DAODrzava mngr;
    
    public DrzavaController() {
    }
    
    @Override
    protected DAODrzava getFacade() {
        if (mngr == null) {
            mngr = new DAODrzava();
        }
        return mngr;
    }
    
    @Override
    protected Class getClassType(){
        return Drzava.class;
    }

    @Override
    public String create() {
        try {
            getFacade().save(selected);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("DrzavaCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String update() {
        try {
            getFacade().update(selected);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("DrzavaUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    @Override
    protected void performDestroy() {
        try {
            getFacade().remove(selected);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("DrzavaDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }
    
    public DataModel getDrzavas() {
        return new ListDataModel( getFacade().findAll() );
    }
    
}
